package com.dyit.springcloud.hospital.alipay.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.dyit.springcloud.hospital.alipay.config.AlipayConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/api/alipay")
public class AliPayController {

    @ResponseBody
    @RequestMapping("/pay")
    public String pay(String out_trade_no,String subject,String total_amount,
                      String body,String time_out,String product_code) throws Exception{
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

        //创建Request请求
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

        //封装传入参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);//商品ID
        model.setSubject(subject);//商品名称
        model.setTotalAmount(total_amount);//支付金额
        model.setBody(body);//商品描述
        model.setTimeoutExpress(time_out);//超市描述
        model.setProductCode(product_code);//商品码

        //设置参数
        request.setBizModel(model);
        //设置异步回调地址
        request.setNotifyUrl(AlipayConfig.notify_url);
        request.setReturnUrl(AlipayConfig.return_url);

        String form = alipayClient.pageExecute(request).getBody();

        System.out.println(form);
        return form;
    }
}
